package control;

import control.bean.ControlBeanConstant;
import control.bean.ControlPropertiesBean;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/20/13
 * Time: 10:35 AM
 * To change this template use File | Settings | File Templates.
 */
public class Radio extends Control {

    java.util.List<JRadioButton> options;
    ButtonGroup optionGroup;

    public Radio() {
        fieldType = ControlBeanConstant.RADIO;
        options = new ArrayList<JRadioButton>();

        options.add(new RadioButton());
        options.add(new RadioButton(true));

        buildControlPanel();
        buildPropertiesPanel();
    }
    @Override
    public Component buildControlPanel() {
        if ( controlPanel != null && controlPanel.getComponentCount() > 0 ) {
            controlPanel.removeAll();
        } else {
            controlPanel = new JPanel(new GridBagLayout());
        }

        label = new JTextField(super.labelText);

        optionGroup = new ButtonGroup();

        GridBagConstraints constraints = new GridBagConstraints();
//
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.ipadx = 130;
        constraints.insets = new Insets(5,5,0,5);
        constraints.anchor = GridBagConstraints.WEST;
        constraints.fill = GridBagConstraints.BOTH;

        controlPanel.add(label, constraints);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.insets = new Insets(5,0,0,5);
        constraints.anchor = GridBagConstraints.NORTH;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        int row = 0;

        for ( JRadioButton current: options ) {
            constraints.gridy = row++;
            optionGroup.add(current);
            controlPanel.add(current, constraints);
        }

        controlPanel.addFocusListener(new ControlFocusAdaptor());
        controlPanel.addMouseListener(new ControlMouseAdaptor());

        label.addFocusListener(new ControlFocusAdaptor());
        label.addMouseListener(new ControlMouseAdaptor());

        return controlPanel;
    }

    @Override
    public Component buildPropertiesPanel() {
        propertiesPanel = new JPanel(new GridBagLayout());

        fieldProperties = new JLabel("Field Properties");

        controlLabel    = new JLabel("Control Name");
        controlName     = new JTextField();

        hint            = new JCheckBox("Hint");
        required        = new JCheckBox("Required");
        readOnly        = new JCheckBox("Read Only");

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
//        constraints.ipadx = 130;
        constraints.ipady = 14;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(fieldProperties, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.ipadx = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(controlLabel, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        //constraints.gridheight = 2;
//        constraints.ipadx = 114;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(5, 0, 0, 0);
        propertiesPanel.add(controlName, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(hint, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(required, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 5;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(readOnly, constraints);

        Component strut = Box.createHorizontalStrut(160);
        constraints.gridy = 6;
        propertiesPanel.add(strut, constraints);

        return propertiesPanel;
    }

    public void addOption() {
        options.add(new RadioButton(true));
        System.out.println("An option has been added need to rebuild list.");
        buildPropertiesPanel();
        //TODO update size
    }

    class RadioButton extends JRadioButton implements ActionListener {

        boolean addOption = false;
        public RadioButton() {
            super("Click to Edit Option");
            addActionListener(this);
        }

        public RadioButton(boolean add) {
            super("Click to Add Option");
            addOption = true;
            addActionListener(this);
        }
        public RadioButton(String name) {
            super(name);
            addActionListener(this);
        }


        @Override
        public void actionPerformed(ActionEvent e) {
            if (addOption) {
                addOption();
                addOption = false;
                //TODO update size
                System.out.println(e.getSource());
            }
            this.setText(JOptionPane.showInputDialog("Input Option"));

        }
    }

    private String concat() {
        String concatOptions = new String();

        for ( JRadioButton currentRadio : options ) {
            concatOptions += currentRadio.getText()+",";
        }

        //trim the last comma away and return
        return concatOptions.substring(0, concatOptions.length()-1);
    }

    public String[] breakdown(String options) {
        return options.split(",");
    }

    public void buildOptionsFromString(String[] input) {
        if ( options == null ) {
            options = new ArrayList<JRadioButton>();
        } else {
            options.clear();
        }

        for ( String current : input ) {
            options.add(new RadioButton(current));
        }

        options.add(new RadioButton(true));
        buildControlPanel();
    }

    public ControlPropertiesBean exportData() {
        super.exportData();

        try {
            controlPropertiesBean.addProperty(ControlBeanConstant.OPTIONS, concat());
        } catch ( Exception e ) {
            System.out.println("Drop down list not set.");
        }

        return controlPropertiesBean;
    }

    public void importData(ControlPropertiesBean controlPropertiesBean) {
        try {
            super.importData(controlPropertiesBean);

            buildOptionsFromString(breakdown(controlPropertiesBean.getProperty(ControlBeanConstant.OPTIONS)));
        } catch ( Exception e ) {
            System.out.println("Loading failed at radio");

        }
    }

}
